package com.wwy.arithmetic.newsuanfa;

/**
 * @author 王伟羽
 * @program: arithmetictest
 * @description: 数组转二叉树
 * @create: 2025/04/07
 */

public class Zhuanhuanerchashu {

    public TreeNode sortedArrayToBST(int[] nums) {
        return getTreeNode(nums, 0, nums.length);
    }

    static TreeNode getTreeNode(int[] nums, int left, int right) {
        if (left > right) {
            return null;
        }
        int centNum = left + right / 2;
        if (centNum == nums.length) {
            return null;
        }
        TreeNode node = new TreeNode();
        node.val = nums[centNum];
        node.left = getTreeNode(nums, left, centNum - 1);
        node.right = getTreeNode(nums, centNum + 1, right);
        return node;
    }

}
